#!/bin/bash

# 修改APP_NAME为云效上的应用名
APP_NAME1=ycl-gateway
APP_NAME2=ycl-system
APP_NAME3=ycl-auth
# 应用端口
APP_PORT1=10003
APP_PORT2=10004
APP_PORT3=10005
# 应用健康检查URL
#由于我这里只有两个微服务模块，统一端口进入，故只需要检查一次就可以了
HEALTH_CHECK_URL2=http://127.0.0.1:${APP_PORT1}/login/hello
HEALTH_CHECK_URL3=http://127.0.0.1:${APP_PORT1}/sysLog/health
# 从package.tgz中解压出来的jar包放到这个目录下
APP_HOME=/home/admin/yuanchangliang
# jar包的名字
JAR_NAME1=${APP_HOME}/${APP_NAME1}/target/${APP_NAME1}.jar
JAR_NAME2=${APP_HOME}/ycl-service/ycl-service-system/target/${APP_NAME2}.jar
JAR_NAME3=${APP_HOME}/${APP_NAME3}/target/${APP_NAME3}.jar
#应用的启动日志
JAVA_OUT1=${APP_HOME}/logs/${APP_NAME1}-start.log
JAVA_OUT2=${APP_HOME}/logs/${APP_NAME2}-start.log
JAVA_OUT3=${APP_HOME}/logs/${APP_NAME3}-start.log
# 等待应用启动的时间
APP_START_TIMEOUT=90
# 脚本会在这个目录下生成nginx-status文件
HEALTH_CHECK_FILE_DIR=/home/admin/status
PROG_NAME=$0
ACTION=$1

# 创建出相关目录
mkdir -p ${HEALTH_CHECK_FILE_DIR}
mkdir -p ${APP_HOME}/logs
usage() {
    echo "Usage: $PROG_NAME {start|stop|restart}"
    exit 2
}

health_check1() {
    exptime=0
    echo "checking ${HEALTH_CHECK_URL3}"
    while true
        do
            status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code}  ${HEALTH_CHECK_URL3}`
            echo "check result is : " $?
            if [ "$?" != "0" ]; then
               echo -n -e "\rapplication not started"
            else
                echo "code is $status_code"
                if [ "$status_code" == "200" ];then
                    break
                fi
            fi
            sleep 1
            ((exptime++))

            echo -e "\rWait app to pass health check: $exptime..."

            if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
                echo 'app start failed'
               exit 1
            fi
        done
    echo "check ${HEALTH_CHECK_URL3} success"
}
health_check2() {
    exptime=0
    echo "checking ${HEALTH_CHECK_URL2}"
    while true
        do
            status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code}  ${HEALTH_CHECK_URL2}`
            echo "check result is : " $?
            if [ "$?" != "0" ]; then
               echo -n -e "\rapplication not started"
            else
                echo "code is $status_code"
                if [ "$status_code" == "200" ];then
                    break
                fi
            fi
            sleep 1
            ((exptime++))

            echo -e "\rWait app to pass health check: $exptime..."

            if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
                echo 'app start failed'
               exit 1
            fi
        done
    echo "check ${HEALTH_CHECK_URL2} success"
}

start_application1() {
    echo "starting ${JAR_NAME1} process"
    nohup java -jar ${JAR_NAME1} > ${JAVA_OUT1} 2>&1 &
    echo "started ${JAR_NAME1} process"
}
start_application2() {
    echo "starting ${JAR_NAME2} process"
    nohup java -jar ${JAR_NAME2} > ${JAVA_OUT2} 2>&1 &
    echo "started ${JAR_NAME2} process"
}
start_application3() {
    echo "starting ${JAR_NAME3} process"
    nohup java -jar ${JAR_NAME3} > ${JAVA_OUT3} 2>&1 &
    echo "started ${JAR_NAME3} process"
}

stop_application() {
   checkjavapid=`ps -ef | grep java | grep 'ycl' | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`

   if [[ ! $checkjavapid ]];then
      echo -e "\rno java process"
      return
   fi

   echo "stop java process"
   times=60
   for e in $(seq 60)
   do
        sleep 1
        COSTTIME=$(($times - $e ))
        checkjavapid=`ps -ef | grep java | grep 'ycl' | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
        if [[ $checkjavapid ]];then
            kill -9 $checkjavapid
            echo -e  "\r        -- stopping java lasts `expr $COSTTIME` seconds."
        else
            echo -e "\rjava process has exited"
            break;
        fi
   done
   echo ""
}

start() {
    start_application1
    start_application3
    start_application2
    health_check1
    health_check2
}
stop() {
    stop_application
}
case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    *)
        usage
    ;;
esac
